﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace benefit.Controllers
{
    public class StageSummaryController : BenefitHelper.Tools.BaseControl
    {
        //
        // GET: /StageSummary/

        public ActionResult Index(int uid, string pdate)
        {
            BenefitHelper.Sys.Operator op = db.Operators.Find(uid);
            ViewBag.op = op;

            Common.Web.Cookie cookie = new Common.Web.Cookie();
            ViewBag.datequery = cookie.GetCookie("querydate");
            ViewBag.pdate = cookie.GetCookie("querydate");
            int operId = Convert.ToInt32(cookie.GetCookie("LoginUserId"));
            if (BenefitHelper.Sys.VisitPrower.HasOperatorPrower(operId, uid, db))
            {
                if (pdate.Length > 4)//查询月数据
                {
                    ViewBag.queryyear = pdate.Substring(0, 4);
                    ViewBag.querymonth = pdate.Substring(4, 2);
                    int month = Convert.ToInt32(pdate.Substring(4, 2));
                    int year = Convert.ToInt32(pdate.Substring(0, 4));
                    if (op.OperatorTradedAtMonth(op.Id, month, year, db))
                    {
                        BenefitHelper.Sys.TradeHistory th = new BenefitHelper.Sys.TradeHistory();
                        string lastdate = th.GetLastTradeHistoryDateString(db);
                        int tid = th.GetLastTradeHistoryId(db);

                        ViewBag.lastdate = lastdate;
                        ViewBag.queryuser = uid;


                        List<BenefitHelper.View.OperatorMonthTick> winTicks = new BenefitHelper.View.OperatorMonthTick().GetOperatorMonthWinOrLostTicks(uid, month, year, true, db);
                        List<BenefitHelper.View.OperatorMonthTick> loseTicks = new BenefitHelper.View.OperatorMonthTick().GetOperatorMonthWinOrLostTicks(uid, month, year, false, db);
                        ViewBag.winticks = winTicks;
                        ViewBag.loseticks = loseTicks;


                        BenefitHelper.View.OperatorMonthProductInfo productinfo = new BenefitHelper.View.OperatorMonthProductInfo();
                        List<BenefitHelper.View.OperatorMonthProductInfo> productinfos = productinfo.GetSortedOperatorMonthProductInfo(month, year, uid, db);
                        ViewBag.productinfos = productinfos;

                        BenefitHelper.View.OperatorMonthMoney operatordaymoney = new BenefitHelper.View.OperatorMonthMoney();
                        operatordaymoney.GetOperatorMonthMoney(month, year, uid, db);

                        var operatorandaccountinfo = new BenefitHelper.Data.Month.AccountMonthMoney().GetAccountAndAllChange(uid, month, year, db);
                        ViewBag.operatorandaccountinfo = operatorandaccountinfo;


                        string js = BenefitHelper.Tools.ChartAtManager.GetOperatorMonthYieldString(month, year, uid, db);
                        js += BenefitHelper.Tools.ChartAtManager.MonthDiffBar(month, year, uid, db);
                        ViewBag.js = js;
                    }
                    else
                    {
                        return RedirectToAction("Index/" + uid + "/" + pdate, "Error", null);
                    }
                }
                else//查询年数据
                {
                    ViewBag.queryyear = pdate.Substring(0, 4);
                    ViewBag.querymonth = "";

                    int year = Convert.ToInt32(pdate.Substring(0, 4));
                    BenefitHelper.Sys.TradeHistory th = new BenefitHelper.Sys.TradeHistory();
                    string lastdate = th.GetLastTradeHistoryDateString(db);
                    int tid = th.GetLastTradeHistoryId(db);
                    ViewBag.lastdate = lastdate;
                    ViewBag.queryuser = uid;


                    List<BenefitHelper.View.OperatorYearTick> winTicks = new BenefitHelper.View.OperatorYearTick().GetOperatorYearWinOrLostTicks(uid, year, true, db);
                    List<BenefitHelper.View.OperatorYearTick> loseTicks = new BenefitHelper.View.OperatorYearTick().GetOperatorYearWinOrLostTicks(uid, year, false, db);
                    ViewBag.winticks = winTicks;
                    ViewBag.loseticks = loseTicks;


                    BenefitHelper.View.OperatorYearProductInfo productinfo = new BenefitHelper.View.OperatorYearProductInfo();
                    List<BenefitHelper.View.OperatorYearProductInfo> productinfos = productinfo.GetSortedOperatorYearProductInfo(year, uid, db);
                    ViewBag.productinfos = productinfos;

                    BenefitHelper.View.OperatorYearMoney operatordaymoney = new BenefitHelper.View.OperatorYearMoney();
                    operatordaymoney.GetOperatorYearMoney(year, uid, db);

                    var operatorandaccountinfo = new BenefitHelper.Data.Year.AccountYearMoney().GetAccountAndAllChange(uid, year, db);
                    ViewBag.operatorandaccountinfo = operatorandaccountinfo;


                    string js = BenefitHelper.Tools.ChartAtManager.GetOperatorYearYieldString(year, uid, db);
                    js += BenefitHelper.Tools.ChartAtManager.YearDiffBar(year, uid, db);
                    ViewBag.js = js;
                }
                ViewBag.queryuser = uid;
            }
            else
            {
                db.Dispose();
                return Redirect("/Error/HasNoPrower/" + uid + "/" + pdate);
            }
            return View();
        }

    }
}
